<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfGuardValidatorUser.class.php 12075 2008-10-08 16:15:03Z noel $
 */
class sfGuardValidatorPassword extends sfValidatorBase {
    public function configure($options = array(), $messages = array()) {
	$this->addOption('pwd1_field', 'ancien password');
	$this->addOption('pwd2_field', 'nouveau password');
	$this->addOption('pwd3_field', 'nouveau password (répéter)');

	$this->setMessage('invalid', 'Le nouveau password n\'est pas le même dans les deux champs.');
	$this->addMessage('second', 'Le password entré ne corresponds pas au password de l\'utilisateur connecté');
    }

    protected function doClean($values) {
	$pwd1 = isset($values[$this->getOption('pwd1_field')]) ? $values[$this->getOption('pwd1_field')] : '';
	$pwd2 = isset($values[$this->getOption('pwd2_field')]) ? $values[$this->getOption('pwd2_field')] : '';
	$pwd3 = isset($values[$this->getOption('pwd3_field')]) ? $values[$this->getOption('pwd3_field')] : '';

	// user exists?
	$user = sfContext::getInstance()->getUser()->getGuardUser();
	// password is ok?
	if (! $user->checkPassword($pwd1))
	    throw new sfValidatorErrorSchema($this, array($this->getOption('pwd1_field') => new sfValidatorError($this, 'second')));

	if ( $pwd2 !=  $pwd3 )
	    throw new sfValidatorErrorSchema($this, array($this->getOption('pwd2_field') => new sfValidatorError($this, 'invalid')));

	return array_merge($values, array('password' => $pwd2));
    }
}
